package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.autoRun.ComRunner;
import com.example.onlinemonitoring.model.Oilfxsensor;
import com.example.onlinemonitoring.model.Sf6sensor;
import com.example.onlinemonitoring.model.Oilfxhistory;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.serviceImpl.OilfxsensorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;
import com.example.onlinemonitoring.Util.IEEE754Utils;

import java.net.SocketException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*这里beanName需要和数据库Appdevice中的simple对应并加上Com,不对应请自行修改
    例:simple:dlhl
    @Service("dlhlCom")
*/
@Service("oilfxCom")
@Scope("prototype")
public class OilfxCom extends CommPortManager {
    @Autowired
    private OilfxsensorServiceImpl oilfxsensorServiceImpl;

    @Override
    public void receiveDatas(String str) {
        if (ad.getCommunicationmode() == 4) {
            if (ad.getBrand().equals("DGA9"))
                read61850(str);
            else
                read61850DGA3(str);
        } else {
            if (ad.getBrand().equals("DGA9"))
                deelData(str);
            else
                deelDataDGA3(str);

        }
    }

    public void deelDataDGA3(String str) {
        try {
            str = str.toUpperCase();
            setText(str);
            // 条件判断自己写
            String addrstr = str.substring(0, 2);
            Integer addrint = parseHex4(addrstr);

            reSend[addrindex] = 0;
            addrindex++;
            if (str.length() < 12)
                return;
            Integer startIndex = 6;// 数据起始位置
            Float CO = IEEE754Utils.strbytesToSingle(str.substring(startIndex, startIndex + 8)).floatValue();
             //Float CO= Float.parseFloat(str.substring(startIndex, startIndex+8));
            Float H2 = IEEE754Utils.strbytesToSingle(str.substring(startIndex + 8, startIndex + 16)).floatValue();
            Float H2o = IEEE754Utils.strbytesToSingle(str.substring(startIndex + 16, startIndex + 24)).floatValue();
            Float oiltemp = IEEE754Utils.strbytesToSingle(str.substring(startIndex + 24, startIndex + 32)).floatValue();
            // Float temper = Float.parseFloat(str.substring(startIndex+32, startIndex+40));
            Float H2oRH = IEEE754Utils.strbytesToSingle(str.substring(startIndex + 40, startIndex + 48)).floatValue();

            // Float C2H2 = parseHex4(str.substring(startIndex+4, startIndex+8)) / 10f;
            // Float C2H4 = parseHex4(str.substring(startIndex+8, startIndex+12)) / 10f;

            // Float CO2 = parseHex4(str.substring(startIndex+16, startIndex+20)) / 10f;
            // Float C2H6 = parseHex4(str.substring(startIndex+20, startIndex+24)) / 10f;
            // Float CH4 = parseHex4(str.substring(startIndex+24, startIndex+28)) / 10f;
            // Float O2 = parseHex4(str.substring(startIndex+28, startIndex+32)) / 10f;
            // Float N2 = parseHex4(str.substring(startIndex+28, startIndex+32)) / 10f;

            // Float PPM = parseHex4(str.substring(startIndex+104, startIndex+108)) / 10f;

            // Float TotHyd = parseHex4(str.substring(startIndex+112, startIndex+116)) /
            // 10f;

            // Float COFrmRate = Float.parseFloat(str.substring(startIndex+64,
            // startIndex+72));
            // Float H2FrmRate = Float.parseFloat(str.substring(startIndex+72,
            // startIndex+80));
            // Float H2OFrmRate = Float.parseFloat(str.substring(startIndex+80,
            // startIndex+88));

            setText("设备返回数据： 站点号：" + ad.getStationid() + " 设备组：" + ad.getDevicegroup() + " 设备地址：" + addrint);
            setText("H2：" + H2 + "ppm");
            // setText("C2H2：" + C2H2 + "ppm");
            // setText("C2H4：" + C2H4 + "ppm");
            setText("CO：" + CO + "ppm");
            // setText("CO2：" + CO2 + "ppm");
            // setText("C2H6：" + C2H6 + "ppm");
            // setText("CH4：" + CH4 + "ppm");
            // setText("O2：" + O2 + "ppm");
            // setText("N2：" + N2 + "ppm");
            // setText("水温：" + temper + "℃");
            setText("油温：" + oiltemp + "℃");
            // setText("油内湿度：" + PPM + "ppm");
            setText("微水值：" + H2o + "ppm");
            setText("油内水含量：" + H2oRH + "%");
            // setText("油内总气体含量：" + TotHyd + "ppm");

            Oilfxsensor oilfxsensor = new Oilfxsensor();
            oilfxsensor.setStationid(ad.getStationid());
            oilfxsensor.setDevicegroup(ad.getDevicegroup());
            oilfxsensor.setDeviceaddr(addrint);
            oilfxsensor = oilfxsensorServiceImpl.getModel(oilfxsensor);

            if (oilfxsensor == null) {
                setText("当前数据库无监测点：" + addrint + " ！ 请先添加该监测点！");
                return;
            }

            oilfxsensor.setLastco(CO);
            oilfxsensor.setLasth2(H2);
            oilfxsensor.setLasth2o(H2oRH);
            oilfxsensor.setLastppm(H2o);
            oilfxsensor.setLastoiltemp(oiltemp);
            // oilfxsensor.setLastcofrmrate(COFrmRate);
            // oilfxsensor.setLasth2frmrate(H2FrmRate);
            // oilfxsensor.setLasth2ofrmrate(H2OFrmRate);
            AddAndUpdate(oilfxsensor);

        } catch (Exception ex) {
            ex.printStackTrace();
            setText("从设备返回的数据有误！请检查串口！");
        }
    }

    public void deelData(String str) {
        try {
            str = str.toUpperCase();
            setText(str);
            // 条件判断自己写
            String addrstr = str.substring(0, 2);
            Integer addrint = parseHex4(addrstr);

            reSend[addrindex] = 0;
            addrindex++;
            if (str.length() < 12)
                return;
            Integer startIndex = 6;// 数据起始位置

            Float H2 = parseHex4(str.substring(startIndex, startIndex + 4)) / 10f;
            Float C2H2 = parseHex4(str.substring(startIndex + 4, startIndex + 8)) / 10f;
            Float C2H4 = parseHex4(str.substring(startIndex + 8, startIndex + 12)) / 10f;
            Float CO = parseHex4(str.substring(startIndex + 12, startIndex + 16)) / 10f;
            Float CO2 = parseHex4(str.substring(startIndex + 16, startIndex + 20)) / 10f;
            Float C2H6 = parseHex4(str.substring(startIndex + 20, startIndex + 24)) / 10f;
            Float CH4 = parseHex4(str.substring(startIndex + 24, startIndex + 28)) / 10f;
            Float O2 = parseHex4(str.substring(startIndex + 28, startIndex + 32)) / 10f;
            Float N2 = parseHex4(str.substring(startIndex + 28, startIndex + 32)) / 10f;
            Float temper = parseHex4(str.substring(startIndex + 96, startIndex + 100)) / 10f;
            Float oiltemp = parseHex4(str.substring(startIndex + 100, startIndex + 104)) / 10f;
            Float PPM = parseHex4(str.substring(startIndex + 104, startIndex + 108)) / 10f;
            Float H2o = parseHex4(str.substring(startIndex + 108, startIndex + 112)) / 10f;
            Float TotHyd = parseHex4(str.substring(startIndex + 112, startIndex + 116)) / 10f;
            setText("设备返回数据： 站点号：" + ad.getStationid() + " 设备组：" + ad.getDevicegroup() + " 设备地址：" + addrint);
            setText("H2：" + H2 + "ppm");
            setText("C2H2：" + C2H2 + "ppm");
            setText("C2H4：" + C2H4 + "ppm");
            setText("CO：" + CO + "ppm");
            setText("CO2：" + CO2 + "ppm");
            setText("C2H6：" + C2H6 + "ppm");
            setText("CH4：" + CH4 + "ppm");
            setText("O2：" + O2 + "ppm");
            setText("N2：" + N2 + "ppm");
            setText("水温：" + temper + "℃");
            setText("油温：" + oiltemp + "℃");
            setText("油内湿度：" + PPM + "ppm");
            setText("油内水含量：" + H2o + "%");
            setText("油内总气体含量：" + TotHyd + "ppm");

            Oilfxsensor oilfxsensor = new Oilfxsensor();
            oilfxsensor.setStationid(ad.getStationid());
            oilfxsensor.setDevicegroup(ad.getDevicegroup());
            oilfxsensor.setDeviceaddr(addrint);
            oilfxsensor = oilfxsensorServiceImpl.getModel(oilfxsensor);

            if (oilfxsensor == null) {
                setText("当前数据库无监测点：" + addrint + " ！ 请先添加该监测点！");
                return;
            }

            oilfxsensor.setLasth2(H2);
            oilfxsensor.setLastc2h2(C2H2);
            oilfxsensor.setLastc2h4(C2H4);
            oilfxsensor.setLastco(CO);
            oilfxsensor.setLastco2(CO2);
            oilfxsensor.setLastc2h6(C2H6);
            oilfxsensor.setLastch4(CH4);
            oilfxsensor.setLasto2(O2);
            oilfxsensor.setLastn2(N2);
            oilfxsensor.setLasttemperature(temper);
            oilfxsensor.setLastoiltemp(oiltemp);
            oilfxsensor.setLastppm(PPM);
            oilfxsensor.setLasth2o(H2o);
            oilfxsensor.setLasttothyd(TotHyd);
            AddAndUpdate(oilfxsensor);

        } catch (Exception ex) {
            ex.printStackTrace();
            setText("从设备返回的数据有误！请检查串口！");
        }
    }

    @Transactional
    private void AddAndUpdate(Oilfxsensor record) {
        int alarmLevel = 0;// 报警等级
        String alarmMessage = "";// 报警消息
        int AlarmStatus[] = new int[13];
        // 报警逻辑
        // 这里写报警逻辑
        if (record.getH2warnvalue() == null || record.getCowarnvalue() == null
                || record.getCh4warnvalue() == null
                || record.getCo2warnvalue() == null || record.getC2h4warnvalue() == null
                || record.getC2h2warnvalue() == null || record.getC2h6warnvalue() == null
                || record.getTothydwarnvalue() == null || record.getPpmalarmvalue() == null
                || record.getO2warnvalue() == null
                || record.getH2alarmvalue() == null || record.getCoalarmvalue() == null
                || record.getCh4alarmvalue() == null || record.getCo2alarmvalue() == null
                || record.getC2h4alarmvalue() == null || record.getC2h2alarmvalue() == null
                || record.getC2h6alarmvalue() == null || record.getTothydalarmvalue() == null
                || record.getPpmalarmvalue() == null
                || record.getO2alarmvalue() == null) {
            setText("设备地址" + record.getDeviceaddr() + " 未设置报警值！");
            return;
        }
        if (ad.getBrand().equals("DGA9"))
        {
            AlarmStatus[0] = GetAlarmStatus(isnull(record.getLasth2()), record.getH2alarmvalue(),
                    record.getH2alarmrelievevalue(),
                    record.getH2warnvalue(), record.getH2warnrelievevalue(), isnull(record.getLasth2alarmstatus()), true,
                    "氢气",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[1] = GetAlarmStatus(isnull(record.getLastco()), record.getCoalarmvalue(),
                    record.getCoalarmrelievevalue(),
                    record.getCowarnvalue(), record.getCowarnvalue(), isnull(record.getLastcoalarmstatus()), true, "一氧化碳",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[2] = GetAlarmStatus(isnull(record.getLastch4()), record.getCh4alarmvalue(),
                    record.getCh4alarmrelievevalue(),
                    record.getCh4warnvalue(), record.getCh4warnrelievevalue(), isnull(record.getLastch4alarmstatus()), true,
                    "甲烷",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[3] = GetAlarmStatus(isnull(record.getLastco2()), record.getCo2alarmvalue(),
                    record.getCo2alarmrelievevalue(),
                    record.getCo2warnvalue(), record.getCo2warnrelievevalue(), isnull(record.getLastco2alarmstatus()), true,
                    "二氧化碳",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[4] = GetAlarmStatus(isnull(record.getLastc2h4()), record.getC2h4alarmvalue(),
                    record.getC2h4alarmrelievevalue(),
                    record.getC2h4warnvalue(), record.getC2h4warnrelievevalue(), isnull(record.getLastc2h4alarmstatus()),
                    true, "乙烯",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[5] = GetAlarmStatus(isnull(record.getLastc2h2()), record.getC2h2alarmvalue(),
                    record.getC2h2alarmrelievevalue(),
                    record.getC2h2warnvalue(), record.getC2h2warnrelievevalue(), isnull(record.getLastc2h2alarmstatus()),
                    true, "乙炔",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[6] = GetAlarmStatus(isnull(record.getLastc2h6()), record.getC2h6alarmvalue(),
                    record.getC2h6alarmrelievevalue(),
                    record.getC2h6warnvalue(), record.getC2h6warnrelievevalue(), isnull(record.getLastc2h6alarmstatus()),
                    true, "乙烷",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[7] = GetAlarmStatus(isnull(record.getLasttothyd()), record.getTothydalarmvalue(),
                    record.getTothydalarmrelievevalue(),
                    record.getTothydwarnvalue(), record.getTothydwarnrelievevalue(),
                    isnull(record.getLasttothydalarmstatus()),
                    true, "总可燃气体", record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[8] = GetAlarmStatus(isnull(record.getLastppm()), record.getPpmalarmvalue(),
                    record.getPpmalarmrelievevalue(),
                    record.getPpmwarnvalue(), record.getPpmwarnrelievevalue(), isnull(record.getLastppmalarmstatus()), true,
                    "湿度",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[9] = GetAlarmStatus(isnull(record.getLasto2()),
                    record.getO2alarmvalue(), record.getO2alarmrelievevalue(),
                    record.getO2warnvalue(),
                    record.getO2warnrelievevalue(), isnull(record.getLasto2alarmstatus()), true, "氧气",
                    record.getDevicegroup(),
                    record.getDeviceaddr(), record.getPointname());
        }
        else
        {
            AlarmStatus[0] = GetAlarmStatus(isnull(record.getLasth2()), record.getH2alarmvalue(),
                    record.getH2alarmrelievevalue(),
                    record.getH2warnvalue(), record.getH2warnrelievevalue(), isnull(record.getLasth2alarmstatus()), true,
                    "氢气",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[1] = GetAlarmStatus(isnull(record.getLastco()), record.getCoalarmvalue(),
                    record.getCoalarmrelievevalue(),
                    record.getCowarnvalue(), record.getCowarnvalue(), isnull(record.getLastcoalarmstatus()), true, "一氧化碳",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
            AlarmStatus[8] = GetAlarmStatus(isnull(record.getLastppm()), record.getPpmalarmvalue(),
                    record.getPpmalarmrelievevalue(),
                    record.getPpmwarnvalue(), record.getPpmwarnrelievevalue(), isnull(record.getLastppmalarmstatus()), true,
                    "湿度",
                    record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
        }

        // AlarmStatus[10] = GetAlarmStatus(isnull(record.getLasth2o()),
        // record.getH2oalarmvalue(),
        // record.getH2oalarmrelievevalue(),
        // record.getH2owarnvalue(), record.getH2owarnrelievevalue(),
        // isnull(record.getLasth2oalarmstatus()), true, "微水含量",
        // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
        // AlarmStatus[11] = GetAlarmStatus(isnull(record.getLastoiltemp()),
        // record.getOiltempalarmvalue(),
        // record.getOiltempalarmrelievevalue(),
        // record.getOiltempwarnvalue(), record.getOiltempwarnrelievevalue(),
        // isnull(record.getLastoiltempalarmstatus()), true, "油温",
        // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
        // AlarmStatus[12] = GetAlarmStatus(isnull(record.getLastgastemp()),
        // record.getGastempalarmvalue(),
        // record.getGastempalarmrelievevalue(),
        // record.getGastempwarnvalue(), record.getGastempwarnrelievevalue(),
        // isnull(record.getLastgastempalarmstatus()), true, "气体温度",
        // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
        // 报警消息
        alarmMessage += GetAlarmMessage(AlarmStatus[0], "氢气");
        alarmMessage += GetAlarmMessage(AlarmStatus[1], "一氧化碳");
        alarmMessage += GetAlarmMessage(AlarmStatus[2], "甲烷");
        alarmMessage += GetAlarmMessage(AlarmStatus[3], "二氧化碳");
        alarmMessage += GetAlarmMessage(AlarmStatus[4], "乙烯");
        alarmMessage += GetAlarmMessage(AlarmStatus[5], "乙炔");
        alarmMessage += GetAlarmMessage(AlarmStatus[6], "乙烷");
        alarmMessage += GetAlarmMessage(AlarmStatus[7], "总可燃气体");
        alarmMessage += GetAlarmMessage(AlarmStatus[8], "微水");
        alarmMessage += GetAlarmMessage(AlarmStatus[9], "氧气");
        // alarmMessage += GetAlarmMessage(AlarmStatus[10], "微水含量");
        // alarmMessage += GetAlarmMessage(AlarmStatus[11], "油温");
        // alarmMessage += GetAlarmMessage(AlarmStatus[12], "气体温度");
        record.setLasth2alarmstatus(AlarmStatus[0]);
        record.setLastcoalarmstatus(AlarmStatus[1]);
        record.setLastch4alarmstatus(AlarmStatus[2]);
        record.setLastco2alarmstatus(AlarmStatus[3]);
        record.setLastc2h4alarmstatus(AlarmStatus[4]);
        record.setLastc2h2alarmstatus(AlarmStatus[5]);
        record.setLastc2h6alarmstatus(AlarmStatus[6]);
        record.setLasttothydalarmstatus(AlarmStatus[7]);
        record.setLastppmalarmstatus(AlarmStatus[8]);
        record.setLasto2alarmstatus(AlarmStatus[9]);
        record.setLasth2oalarmstatus(AlarmStatus[10]);
        record.setLastoiltempalarmstatus(AlarmStatus[11]);
        record.setLastgastempalarmstatus(AlarmStatus[12]);

        record.setLastalarmlevel(HexUtil.MAX(AlarmStatus));
        record.setLastalarmmessage(alarmMessage);
        record.setLastcommunicationstatus(0);
        record.setLastdatatime(new Date());
        record.setLasthisdatatime(addHistory(record));
        if (oilfxsensorServiceImpl.Update(record, false) == 0) {
            setText("更新实时数据失败，请检查数据库连接");
        }
    }

    private Date addHistory(Oilfxsensor sensor) {
        if (sensor != null) {
            if (sensor.getLasthisdatatime() == null) {
                add(sensor);
                return new Date();
            } else {
                long lasttime = 0;
                long nowtime = 0;
                try {
                    lasttime = HexUtil.todate(sensor.getLasthisdatatime()).getTime();
                    nowtime = HexUtil.todate(new Date()).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int timersp = 60000;
                if (sensor.getLastalarmlevel() != 0) {
                    timersp = ad.getHisalarmtimersp() * 1000;
                } else {
                    timersp = ad.getHistimersp() * 1000;
                }
                if (lasttime + timersp < nowtime) {
                    add(sensor);
                    return new Date();
                }
            }
        }
        return null;
    }

    private void add(Oilfxsensor sensor) {
        Oilfxhistory his = new Oilfxhistory();
        his.setDeviceaddr(sensor.getDeviceaddr());
        his.setDevicegroup(sensor.getDevicegroup());
        his.setAlarmlevel(sensor.getLastalarmlevel());
        his.setAlarmmessage(sensor.getLastalarmmessage());
        his.setDatatime(sensor.getLastdatatime());
        his.setPointname(sensor.getPointname());
        his.setStationid(sensor.getStationid());
        // 需要添加自己定义的属性
        his.setTemperature(sensor.getLasttemperature());
        his.setCh4(sensor.getLastch4());
        his.setC2h6(sensor.getLastc2h6());
        his.setC2h4(sensor.getLastc2h4());
        his.setC2h2(sensor.getLastc2h2());
        his.setCo(sensor.getLastco());
        his.setCo2(sensor.getLastco2());
        his.setO2(sensor.getLasto2());
        his.setN2(sensor.getLastn2());
        his.setGastemper(sensor.getLastgastemp());
        his.setOiltemp(sensor.getLastoiltemp());
        his.setH2(sensor.getLasth2());
        his.setH2o(sensor.getLasth2o());
        his.setPpm(sensor.getLastppm());
        his.setTothyd(sensor.getLasttothyd());

        his.setCofrmrate(sensor.getLastcofrmrate());
        his.setH2ofrmrate(sensor.getLasth2ofrmrate());
        his.setH2frmrate(sensor.getLasth2frmrate());
        oilfxsensorServiceImpl.Add(his);
    }

    private void read61850DGA3(String str) {
        Oilfxsensor currrecord = new Oilfxsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        List<Oilfxsensor> lastrecordlist = oilfxsensorServiceImpl.getLists(currrecord);
        if (lastrecordlist.size() != 0) {
            if (lastrecordlist.get(0).getStyle61850() != null) {
                String[] style = lastrecordlist.get(0).getStyle61850().split(",");
                for (String s : style) {
                    map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
                }
            }
        }
        int communicationstatus = 0, comstatusTmp = 0, comstatusH2ppm = 0;
        int addrindex = 1;
        for (Oilfxsensor lastrecord : lastrecordlist) {
            currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
            int alarmLevel = 0;// 报警等级
            String alarmMessage = "";// 报警消息

            List<Integer> array = new ArrayList<>();
            array.add(0);
            try {

                // comstatusTmp = isread61850Value(String.format(isnull(map61850.get("Tmp")),
                // addrindex));
                comstatusH2ppm = isread61850Value(String.format(isnull(map61850.get("H2ppm")), addrindex));
                communicationstatus = Math.max(comstatusTmp, comstatusH2ppm);
                // currrecord.setLastoiltemp(0f);
                // currrecord.setLasth2o(0f);
                // currrecord.setLasttemperature(0f);
                // currrecord.setLastoiltemp(
                // read61850FloatValue(String.format(isnull(map61850.get("Tmp")),
                // addrindex)));//油温
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("H2O")), addrindex)));// 微水含量
                currrecord.setLastppm(currrecord.getLasth2o());
                // currrecord.setLasttemperature(
                // read61850FloatValue(String.format(isnull(map61850.get("H2OTmp")),
                // addrindex)));//水温
                currrecord.setLasth2(
                        read61850FloatValue(String.format(isnull(map61850.get("H2ppm")), addrindex)));// 氢气

                currrecord.setLastco(
                        read61850FloatValue(String.format(isnull(map61850.get("COppm")), addrindex)));

                // currrecord.setLasth2ofrmrate(
                // read61850FloatValue(String.format(isnull(map61850.get("H2OFrmRate")),
                // addrindex)));//微水产生速率
                // currrecord.setLasth2frmrate(
                // read61850FloatValue(String.format(isnull(map61850.get("H2FrmRate")),
                // addrindex)));//产生速率
                // currrecord.setLastcofrmrate(
                // read61850FloatValue(String.format(isnull(map61850.get("COFrmRate")),
                // addrindex)));//产生速率

                currrecord.setLasth2oalarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2o()),
                        isnull(lastrecord.getH2oalarmvalue()),
                        isnull(lastrecord.getH2oalarmrelievevalue()),
                        isnull(lastrecord.getH2owarnvalue()),
                        isnull(lastrecord.getH2owarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[0].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasth2oalarmstatus(),
                        lastrecord.getStyle().split(",")[0].split("\\|")[2]);
                array.add(currrecord.getLasth2oalarmstatus());

                currrecord.setLasth2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2()),
                        isnull(lastrecord.getH2alarmvalue()),
                        isnull(lastrecord.getH2alarmrelievevalue()),
                        isnull(lastrecord.getH2warnvalue()),
                        isnull(lastrecord.getH2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[1].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasth2alarmstatus(),
                        lastrecord.getStyle().split(",")[1].split("\\|")[2]);
                array.add(currrecord.getLasth2alarmstatus());

                currrecord.setLastcoalarmstatus(GetAlarmStatus(isnull(currrecord.getLastco()),
                        isnull(lastrecord.getCoalarmvalue()),
                        isnull(lastrecord.getCoalarmrelievevalue()),
                        isnull(lastrecord.getCowarnvalue()),
                        isnull(lastrecord.getCowarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[2].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastcoalarmstatus(),
                        lastrecord.getStyle().split(",")[2].split("\\|")[2]);
                array.add(currrecord.getLastcoalarmstatus());

            } catch (SocketException e) {
                if (Close()) {
                    communicationstatus = 1;
                    oilfxsensorServiceImpl.UpdateStatus(ad, 1);
                    ComRunner.netmap.remove(ad.getId());
                }
            } catch (Exception e) {
                setText("读取数据出错！" + e.toString());
                communicationstatus = 1;
                oilfxsensorServiceImpl.UpdateStatus(ad, 1);
            }

            alarmLevel = HexUtil.MAX(array);
            currrecord.setLastalarmlevel(alarmLevel);
            currrecord.setLastalarmmessage(alarmMessage);

            currrecord.setLastcommunicationstatus(communicationstatus);
            currrecord.setLastdatatime(new Date());
            if (communicationstatus == 0) {
                currrecord.setLasthisdatatime(addHistory(lastrecord));
                oilfxsensorServiceImpl.Update(currrecord, false);
            } else {
                oilfxsensorServiceImpl.UpdateStatus(ad, 1);
            }

            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 站点地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            // str2.append(" 油温：" + currrecord.getLastoiltemp() + "\r\n");
            // str2.append(" 水温：" + currrecord.getLasttemperature() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            // str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            // str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
            // str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            // str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            // str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            // str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 微水含量：" + currrecord.getLasth2o() + "\r\n");
            // str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            // str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");

            // str2.append(" 微水生成速率：" + currrecord.getLasth2ofrmrate() + "\r\n");
            // str2.append(" 氢气生成速率：" + currrecord.getLasth2frmrate() + "\r\n");
            // str2.append(" 一氧化碳生成速率：" + currrecord.getLastcofrmrate() + "\r\n");
            setText(str2.toString());
        }
    }

    // public void deelData(String str) {
    // try {
    // str = str.toUpperCase();
    // setText(str);
    // // 条件判断自己写
    // String addrstr = str.substring(0, 2);
    // Integer addrint = parseHex4(addrstr);

    // reSend[addrindex] = 0;
    // addrindex++;
    // if (str.length() < 12)
    // return;
    // Integer startIndex = 6;// 数据起始位置

    // Float H2 = parseHex4(str.substring(startIndex, startIndex + 4)) / 10f;
    // Float C2H2 = parseHex4(str.substring(startIndex + 4, startIndex + 8)) / 10f;
    // Float C2H4 = parseHex4(str.substring(startIndex + 8, startIndex + 12)) / 10f;
    // Float CO = parseHex4(str.substring(startIndex + 12, startIndex + 16)) / 10f;
    // Float CO2 = parseHex4(str.substring(startIndex + 16, startIndex + 20)) / 10f;
    // Float C2H6 = parseHex4(str.substring(startIndex + 20, startIndex + 24)) /
    // 10f;
    // Float CH4 = parseHex4(str.substring(startIndex + 24, startIndex + 28)) / 10f;
    // Float O2 = parseHex4(str.substring(startIndex + 28, startIndex + 32)) / 10f;
    // Float N2 = parseHex4(str.substring(startIndex + 28, startIndex + 32)) / 10f;
    // Float temper = parseHex4(str.substring(startIndex + 96, startIndex + 100)) /
    // 10f;
    // Float oiltemp = parseHex4(str.substring(startIndex + 100, startIndex + 104))
    // / 10f;
    // Float PPM = parseHex4(str.substring(startIndex + 104, startIndex + 108)) /
    // 10f;
    // Float H2o = parseHex4(str.substring(startIndex + 108, startIndex + 112)) /
    // 10f;
    // Float TotHyd = parseHex4(str.substring(startIndex + 112, startIndex + 116)) /
    // 10f;
    // setText("设备返回数据： 站点号：" + ad.getStationid() + " 设备组：" + ad.getDevicegroup() +
    // " 设备地址：" + addrint);
    // setText("H2：" + H2 + "ppm");
    // setText("C2H2：" + C2H2 + "ppm");
    // setText("C2H4：" + C2H4 + "ppm");
    // setText("CO：" + CO + "ppm");
    // setText("CO2：" + CO2 + "ppm");
    // setText("C2H6：" + C2H6 + "ppm");
    // setText("CH4：" + CH4 + "ppm");
    // setText("O2：" + O2 + "ppm");
    // setText("N2：" + N2 + "ppm");
    // setText("水温：" + temper + "℃");
    // setText("油温：" + oiltemp + "℃");
    // setText("油内湿度：" + PPM + "ppm");
    // setText("油内水含量：" + H2o + "%");
    // setText("油内总气体含量：" + TotHyd + "ppm");

    // Oilfxsensor oilfxsensor = new Oilfxsensor();
    // oilfxsensor.setStationid(ad.getStationid());
    // oilfxsensor.setDevicegroup(ad.getDevicegroup());
    // oilfxsensor.setDeviceaddr(addrint);
    // oilfxsensor = oilfxsensorServiceImpl.getModel(oilfxsensor);

    // if (oilfxsensor == null) {
    // setText("当前数据库无监测点：" + addrint + " ！ 请先添加该监测点！");
    // return;
    // }

    // oilfxsensor.setLasth2(H2);
    // oilfxsensor.setLastc2h2(C2H2);
    // oilfxsensor.setLastc2h4(C2H4);
    // oilfxsensor.setLastco(CO);
    // oilfxsensor.setLastco2(CO2);
    // oilfxsensor.setLastc2h6(C2H6);
    // oilfxsensor.setLastch4(CH4);
    // oilfxsensor.setLasto2(O2);
    // oilfxsensor.setLastn2(N2);
    // oilfxsensor.setLasttemperature(temper);
    // oilfxsensor.setLastoiltemp(oiltemp);
    // oilfxsensor.setLastppm(PPM);
    // oilfxsensor.setLasth2o(H2o);
    // oilfxsensor.setLasttothyd(TotHyd);
    // AddAndUpdate(oilfxsensor);

    // } catch (Exception ex) {
    // ex.printStackTrace();
    // setText("从设备返回的数据有误！请检查串口！");
    // }
    // }

    // @Transactional
    // private void AddAndUpdate(Oilfxsensor record) {
    // int alarmLevel = 0;// 报警等级
    // String alarmMessage = "";// 报警消息
    // int AlarmStatus[] = new int[13];
    // // 报警逻辑
    // // 这里写报警逻辑
    // if (record.getH2warnvalue() == null || record.getCowarnvalue() == null
    // || record.getCh4warnvalue() == null
    // || record.getCo2warnvalue() == null || record.getC2h4warnvalue() == null
    // || record.getC2h2warnvalue() == null || record.getC2h6warnvalue() == null
    // || record.getTothydwarnvalue() == null || record.getPpmalarmvalue() == null
    // || record.getO2warnvalue() == null
    // || record.getH2warnvalue() == null || record.getCoalarmvalue() == null
    // || record.getCh4alarmvalue() == null || record.getCo2alarmvalue() == null
    // || record.getC2h4alarmvalue() == null || record.getC2h2alarmvalue() == null
    // || record.getC2h6alarmvalue() == null || record.getTothydalarmvalue() == null
    // || record.getPpmalarmvalue() == null
    // || record.getO2alarmvalue() == null) {
    // setText("设备地址" + record.getDeviceaddr() + " 未设置报警值！");
    // return;
    // }
    // AlarmStatus[0] = GetAlarmStatus(isnull(record.getLasth2()),
    // record.getH2alarmvalue(),
    // record.getH2alarmrelievevalue(),
    // record.getH2warnvalue(), record.getH2warnrelievevalue(),
    // isnull(record.getLasth2alarmstatus()), true,
    // "氢气",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[1] = GetAlarmStatus(isnull(record.getLastco()),
    // record.getCoalarmvalue(),
    // record.getCoalarmrelievevalue(),
    // record.getCowarnvalue(), record.getCowarnvalue(),
    // isnull(record.getLastcoalarmstatus()), true, "一氧化碳",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[2] = GetAlarmStatus(isnull(record.getLastch4()),
    // record.getCh4alarmvalue(),
    // record.getCh4alarmrelievevalue(),
    // record.getCh4warnvalue(), record.getCh4warnrelievevalue(),
    // isnull(record.getLastch4alarmstatus()), true,
    // "甲烷",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[3] = GetAlarmStatus(isnull(record.getLastco2()),
    // record.getCo2alarmvalue(),
    // record.getCo2alarmrelievevalue(),
    // record.getCo2warnvalue(), record.getCo2warnrelievevalue(),
    // isnull(record.getLastco2alarmstatus()), true,
    // "二氧化碳",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[4] = GetAlarmStatus(isnull(record.getLastc2h4()),
    // record.getC2h4alarmvalue(),
    // record.getC2h4alarmrelievevalue(),
    // record.getC2h4warnvalue(), record.getC2h4warnrelievevalue(),
    // isnull(record.getLastc2h4alarmstatus()),
    // true, "乙烯",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[5] = GetAlarmStatus(isnull(record.getLastc2h2()),
    // record.getC2h2alarmvalue(),
    // record.getC2h2alarmrelievevalue(),
    // record.getC2h2warnvalue(), record.getC2h2warnrelievevalue(),
    // isnull(record.getLastc2h2alarmstatus()),
    // true, "乙炔",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[6] = GetAlarmStatus(isnull(record.getLastc2h6()),
    // record.getC2h6alarmvalue(),
    // record.getC2h6alarmrelievevalue(),
    // record.getC2h6warnvalue(), record.getC2h6warnrelievevalue(),
    // isnull(record.getLastc2h6alarmstatus()),
    // true, "乙烷",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[7] = GetAlarmStatus(isnull(record.getLasttothyd()),
    // record.getTothydalarmvalue(),
    // record.getTothydalarmrelievevalue(),
    // record.getTothydwarnvalue(), record.getTothydwarnrelievevalue(),
    // isnull(record.getLasttothydalarmstatus()),
    // true, "总可燃气体", record.getDevicegroup(), record.getDeviceaddr(),
    // record.getPointname());
    // AlarmStatus[8] = GetAlarmStatus(isnull(record.getLastppm()),
    // record.getPpmalarmvalue(),
    // record.getPpmalarmrelievevalue(),
    // record.getPpmwarnvalue(), record.getPpmwarnrelievevalue(),
    // isnull(record.getLastppmalarmstatus()), true,
    // "湿度",
    // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // AlarmStatus[9] = GetAlarmStatus(isnull(record.getLasto2()),
    // record.getO2alarmvalue(), record.getO2alarmrelievevalue(),
    // record.getO2warnvalue(),
    // record.getO2warnrelievevalue(), isnull(record.getLasto2alarmstatus()), true,
    // "氧气",
    // record.getDevicegroup(),
    // record.getDeviceaddr(), record.getPointname());
    // // AlarmStatus[10] = GetAlarmStatus(isnull(record.getLasth2o()),
    // // record.getH2oalarmvalue(),
    // // record.getH2oalarmrelievevalue(),
    // // record.getH2owarnvalue(), record.getH2owarnrelievevalue(),
    // // isnull(record.getLasth2oalarmstatus()), true, "微水含量",
    // // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // // AlarmStatus[11] = GetAlarmStatus(isnull(record.getLastoiltemp()),
    // // record.getOiltempalarmvalue(),
    // // record.getOiltempalarmrelievevalue(),
    // // record.getOiltempwarnvalue(), record.getOiltempwarnrelievevalue(),
    // // isnull(record.getLastoiltempalarmstatus()), true, "油温",
    // // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // // AlarmStatus[12] = GetAlarmStatus(isnull(record.getLastgastemp()),
    // // record.getGastempalarmvalue(),
    // // record.getGastempalarmrelievevalue(),
    // // record.getGastempwarnvalue(), record.getGastempwarnrelievevalue(),
    // // isnull(record.getLastgastempalarmstatus()), true, "气体温度",
    // // record.getDevicegroup(), record.getDeviceaddr(), record.getPointname());
    // // 报警消息
    // alarmMessage += GetAlarmMessage(AlarmStatus[0], "氢气");
    // alarmMessage += GetAlarmMessage(AlarmStatus[1], "一氧化碳");
    // alarmMessage += GetAlarmMessage(AlarmStatus[2], "甲烷");
    // alarmMessage += GetAlarmMessage(AlarmStatus[3], "二氧化碳");
    // alarmMessage += GetAlarmMessage(AlarmStatus[4], "乙烯");
    // alarmMessage += GetAlarmMessage(AlarmStatus[5], "乙炔");
    // alarmMessage += GetAlarmMessage(AlarmStatus[6], "乙烷");
    // alarmMessage += GetAlarmMessage(AlarmStatus[7], "总可燃气体");
    // alarmMessage += GetAlarmMessage(AlarmStatus[8], "微水");
    // alarmMessage += GetAlarmMessage(AlarmStatus[9], "氧气");
    // // alarmMessage += GetAlarmMessage(AlarmStatus[10], "微水含量");
    // // alarmMessage += GetAlarmMessage(AlarmStatus[11], "油温");
    // // alarmMessage += GetAlarmMessage(AlarmStatus[12], "气体温度");
    // record.setLasth2alarmstatus(AlarmStatus[0]);
    // record.setLastcoalarmstatus(AlarmStatus[1]);
    // record.setLastch4alarmstatus(AlarmStatus[2]);
    // record.setLastco2alarmstatus(AlarmStatus[3]);
    // record.setLastc2h4alarmstatus(AlarmStatus[4]);
    // record.setLastc2h2alarmstatus(AlarmStatus[5]);
    // record.setLastc2h6alarmstatus(AlarmStatus[6]);
    // record.setLasttothydalarmstatus(AlarmStatus[7]);
    // record.setLastppmalarmstatus(AlarmStatus[8]);
    // record.setLasto2alarmstatus(AlarmStatus[9]);
    // record.setLasth2oalarmstatus(AlarmStatus[10]);
    // record.setLastoiltempalarmstatus(AlarmStatus[11]);
    // record.setLastgastempalarmstatus(AlarmStatus[12]);

    // record.setLastalarmlevel(HexUtil.MAX(AlarmStatus));
    // record.setLastalarmmessage(alarmMessage);
    // record.setLastcommunicationstatus(0);
    // record.setLastdatatime(new Date());
    // record.setLasthisdatatime(addHistory(record));
    // if (oilfxsensorServiceImpl.Update(record, false) == 0) {
    // setText("更新实时数据失败，请检查数据库连接");
    // }
    // }

    // private Date addHistory(Oilfxsensor sensor) {
    // if (sensor != null) {
    // if (sensor.getLasthisdatatime() == null) {
    // add(sensor);
    // return new Date();
    // } else {
    // long lasttime = 0;
    // long nowtime = 0;
    // try {
    // lasttime = HexUtil.todate(sensor.getLasthisdatatime()).getTime();
    // nowtime = HexUtil.todate(new Date()).getTime();
    // } catch (ParseException e) {
    // e.printStackTrace();
    // }
    // int timersp = 60000;
    // if (sensor.getLastalarmlevel() != 0) {
    // timersp = ad.getHisalarmtimersp() * 1000;
    // } else {
    // timersp = ad.getHistimersp() * 1000;
    // }
    // if (lasttime + timersp < nowtime) {
    // add(sensor);
    // return new Date();
    // }
    // }
    // }
    // return null;
    // }

    // private void add(Oilfxsensor sensor) {
    // Oilfxhistory his = new Oilfxhistory();
    // his.setDeviceaddr(sensor.getDeviceaddr());
    // his.setDevicegroup(sensor.getDevicegroup());
    // his.setAlarmlevel(sensor.getLastalarmlevel());
    // his.setAlarmmessage(sensor.getLastalarmmessage());
    // his.setDatatime(sensor.getLastdatatime());
    // his.setPointname(sensor.getPointname());
    // his.setStationid(sensor.getStationid());
    // // 需要添加自己定义的属性
    // his.setTemperature(sensor.getLasttemperature());
    // his.setCh4(sensor.getLastch4());
    // his.setC2h6(sensor.getLastc2h6());
    // his.setC2h4(sensor.getLastc2h4());
    // his.setC2h2(sensor.getLastc2h2());
    // his.setCo(sensor.getLastco());
    // his.setCo2(sensor.getLastco2());
    // his.setO2(sensor.getLasto2());
    // his.setN2(sensor.getLastn2());
    // his.setGastemper(sensor.getLastgastemp());
    // his.setOiltemp(sensor.getLastoiltemp());
    // his.setH2(sensor.getLasth2());
    // his.setH2o(sensor.getLasth2o());
    // his.setPpm(sensor.getLastppm());
    // his.setTothyd(sensor.getLasttothyd());

    // his.setCofrmrate(sensor.getLastcofrmrate());
    // his.setH2ofrmrate(sensor.getLasth2ofrmrate());
    // his.setH2frmrate(sensor.getLasth2frmrate());
    // oilfxsensorServiceImpl.Add(his);
    // }

    // private void read61850DGA3(String str) {
    // Oilfxsensor currrecord = new Oilfxsensor();
    // currrecord.setStationid(ad.getStationid());
    // currrecord.setDevicegroup(ad.getDevicegroup());
    // List<Oilfxsensor> lastrecordlist =
    // oilfxsensorServiceImpl.getLists(currrecord);
    // if (lastrecordlist.size() != 0) {
    // if (lastrecordlist.get(0).getStyle61850() != null) {
    // String[] style = lastrecordlist.get(0).getStyle61850().split(",");
    // for (String s : style) {
    // map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
    // }
    // }
    // }
    // int communicationstatus = 0, comstatusTmp = 0, comstatusH2ppm = 0;
    // int addrindex = 1;
    // for (Oilfxsensor lastrecord : lastrecordlist) {
    // currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
    // int alarmLevel = 0;// 报警等级
    // String alarmMessage = "";// 报警消息

    // List<Integer> array = new ArrayList<>();
    // array.add(0);
    // try {

    // // comstatusTmp = isread61850Value(String.format(isnull(map61850.get("Tmp")),
    // // addrindex));
    // comstatusH2ppm =
    // isread61850Value(String.format(isnull(map61850.get("H2ppm")), addrindex));
    // communicationstatus = Math.max(comstatusTmp, comstatusH2ppm);
    // // currrecord.setLastoiltemp(0f);
    // // currrecord.setLasth2o(0f);
    // // currrecord.setLasttemperature(0f);
    // // currrecord.setLastoiltemp(
    // // read61850FloatValue(String.format(isnull(map61850.get("Tmp")),
    // // addrindex)));//油温
    // currrecord.setLasth2o(
    // read61850FloatValue(String.format(isnull(map61850.get("H2O")),
    // addrindex)));// 微水含量
    // currrecord.setLastppm(currrecord.getLasth2o());
    // // currrecord.setLasttemperature(
    // // read61850FloatValue(String.format(isnull(map61850.get("H2OTmp")),
    // // addrindex)));//水温
    // currrecord.setLasth2(
    // read61850FloatValue(String.format(isnull(map61850.get("H2ppm")),
    // addrindex)));// 氢气

    // currrecord.setLastco(
    // read61850FloatValue(String.format(isnull(map61850.get("COppm")),
    // addrindex)));

    // currrecord.setLasth2ofrmrate(
    // read61850FloatValue(String.format(isnull(map61850.get("H2OFrmRate")),
    // addrindex)));// 微水产生速率
    // currrecord.setLasth2frmrate(
    // read61850FloatValue(String.format(isnull(map61850.get("H2FrmRate")),
    // addrindex)));// 产生速率
    // currrecord.setLastcofrmrate(
    // read61850FloatValue(String.format(isnull(map61850.get("COFrmRate")),
    // addrindex)));// 产生速率

    // currrecord.setLasth2oalarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2o()),
    // isnull(lastrecord.getH2oalarmvalue()),
    // isnull(lastrecord.getH2oalarmrelievevalue()),
    // isnull(lastrecord.getH2owarnvalue()),
    // isnull(lastrecord.getH2owarnrelievevalue()),
    // isnull(lastrecord.getLastalarmlevel()), true,
    // lastrecord.getStyle().split(",")[0].split("\\|")[2],
    // lastrecord.getDevicegroup(),
    // lastrecord.getDeviceaddr(),
    // lastrecord.getPointname()));
    // alarmMessage += GetAlarmMessage(currrecord.getLasth2oalarmstatus(),
    // lastrecord.getStyle().split(",")[0].split("\\|")[2]);
    // array.add(currrecord.getLasth2oalarmstatus());

    // currrecord.setLasth2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2()),
    // isnull(lastrecord.getH2alarmvalue()),
    // isnull(lastrecord.getH2alarmrelievevalue()),
    // isnull(lastrecord.getH2warnvalue()),
    // isnull(lastrecord.getH2warnrelievevalue()),
    // isnull(lastrecord.getLastalarmlevel()), true,
    // lastrecord.getStyle().split(",")[1].split("\\|")[2],
    // lastrecord.getDevicegroup(),
    // lastrecord.getDeviceaddr(),
    // lastrecord.getPointname()));
    // alarmMessage += GetAlarmMessage(currrecord.getLasth2alarmstatus(),
    // lastrecord.getStyle().split(",")[1].split("\\|")[2]);
    // array.add(currrecord.getLasth2alarmstatus());

    // currrecord.setLastcoalarmstatus(GetAlarmStatus(isnull(currrecord.getLastco()),
    // isnull(lastrecord.getCoalarmvalue()),
    // isnull(lastrecord.getCoalarmrelievevalue()),
    // isnull(lastrecord.getCowarnvalue()),
    // isnull(lastrecord.getCowarnrelievevalue()),
    // isnull(lastrecord.getLastalarmlevel()), true,
    // lastrecord.getStyle().split(",")[2].split("\\|")[2],
    // lastrecord.getDevicegroup(),
    // lastrecord.getDeviceaddr(),
    // lastrecord.getPointname()));
    // alarmMessage += GetAlarmMessage(currrecord.getLastcoalarmstatus(),
    // lastrecord.getStyle().split(",")[2].split("\\|")[2]);
    // array.add(currrecord.getLastcoalarmstatus());

    // } catch (SocketException e) {
    // if (Close()) {
    // communicationstatus = 1;
    // oilfxsensorServiceImpl.UpdateStatus(ad, 1);
    // ComRunner.netmap.remove(ad.getId());
    // }
    // } catch (Exception e) {
    // setText("读取数据出错！" + e.toString());
    // communicationstatus = 1;
    // oilfxsensorServiceImpl.UpdateStatus(ad, 1);
    // }

    // alarmLevel = HexUtil.MAX(array);
    // currrecord.setLastalarmlevel(alarmLevel);
    // currrecord.setLastalarmmessage(alarmMessage);

    // currrecord.setLastcommunicationstatus(communicationstatus);
    // currrecord.setLastdatatime(new Date());
    // if (communicationstatus == 0) {
    // currrecord.setLasthisdatatime(addHistory(lastrecord));
    // oilfxsensorServiceImpl.Update(currrecord, false);
    // } else {
    // oilfxsensorServiceImpl.UpdateStatus(ad, 1);
    // }

    // StringBuilder str2 = new StringBuilder();
    // str2.append("【设备返回数据：】");
    // str2.append(" 站点地址：" + currrecord.getStationid());
    // str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
    // // str2.append(" 油温：" + currrecord.getLastoiltemp() + "\r\n");
    // // str2.append(" 水温：" + currrecord.getLasttemperature() + "\r\n");
    // str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
    // str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
    // // str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
    // // str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
    // // str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
    // // str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
    // // str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
    // // str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
    // str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
    // // str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
    // // str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");

    // str2.append(" 微水生成速率：" + currrecord.getLasth2ofrmrate() + "\r\n");
    // str2.append(" 氢气生成速率：" + currrecord.getLasth2frmrate() + "\r\n");
    // str2.append(" 一氧化碳生成速率：" + currrecord.getLastcofrmrate() + "\r\n");
    // setText(str2.toString());
    // }
    // }

    private void read61850(String str) {
        Oilfxsensor currrecord = new Oilfxsensor();
        currrecord.setStationid(ad.getStationid());
        currrecord.setDevicegroup(ad.getDevicegroup());
        List<Oilfxsensor> lastrecordlist = oilfxsensorServiceImpl.getLists(currrecord);
        if (lastrecordlist.size() != 0) {
            if (lastrecordlist.get(0).getStyle61850() != null) {
                String[] style = lastrecordlist.get(0).getStyle61850().split(",");
                for (String s : style) {
                    map61850.put(s.split("\\|")[0], s.split("\\|")[1]);
                }
            }
        }
        int communicationstatus = 0, comstatusTmp = 0, comstatusH2ppm = 0;
        int addrindex = 0;
        for (Oilfxsensor lastrecord : lastrecordlist) {
            currrecord.setDeviceaddr(lastrecord.getDeviceaddr());
            int alarmLevel = 0;// 报警等级
            String alarmMessage = "";// 报警消息

            List<Integer> array = new ArrayList<>();
            array.add(0);
            try {

                comstatusTmp = isread61850Value(String.format(isnull(map61850.get("Tmp")), addrindex));
                comstatusH2ppm = isread61850Value(String.format(isnull(map61850.get("H2ppm")), addrindex));
                communicationstatus = Math.max(comstatusTmp, comstatusH2ppm);
                // currrecord.setLastoiltemp(0f);
                // currrecord.setLasth2o(0f);
                // currrecord.setLasttemperature(0f);
                currrecord.setLastoiltemp(
                        read61850FloatValue(String.format(isnull(map61850.get("Tmp")), addrindex)));// 油温
                currrecord.setLasth2o(
                        read61850FloatValue(String.format(isnull(map61850.get("H2O")), addrindex)));// 微水含量
                currrecord.setLastppm(currrecord.getLasth2o());
                currrecord.setLasttemperature(
                        read61850FloatValue(String.format(isnull(map61850.get("H2OTmp")), addrindex)));// 水温
                currrecord.setLasth2(
                        read61850FloatValue(String.format(isnull(map61850.get("H2ppm")), addrindex)));// 氢气
                currrecord.setLastc2h2(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H2ppm")), addrindex)));
                currrecord.setLastc2h4(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H4ppm")), addrindex)));
                currrecord.setLastco(
                        read61850FloatValue(String.format(isnull(map61850.get("COppm")), addrindex)));
                currrecord.setLastco2(
                        read61850FloatValue(String.format(isnull(map61850.get("CO2ppm")), addrindex)));
                currrecord.setLastc2h6(read61850FloatValue(
                        String.format(isnull(map61850.get("C2H6ppm")), addrindex)));
                currrecord.setLastch4(
                        read61850FloatValue(String.format(isnull(map61850.get("CH4ppm")), addrindex)));
                currrecord.setLasto2(
                        read61850FloatValue(String.format(isnull(map61850.get("O2ppm")), addrindex)));
                currrecord.setLastn2(
                        read61850FloatValue(String.format(isnull(map61850.get("N2ppm")), addrindex)));
                currrecord.setLasttothyd(
                        read61850FloatValue(String.format(isnull(map61850.get("CmbuGas")), addrindex)));

                // currrecord.setLastn2(
                // read61850FloatValue(String.format(isnull(map61850.get("N2ppm")),
                // lastrecord.getDeviceaddr())));

                currrecord.setLastoiltempalarmstatus(GetAlarmStatus(isnull(currrecord.getLastoiltemp()),
                        isnull(lastrecord.getOiltempalarmvalue()),
                        isnull(lastrecord.getOiltempalarmrelievevalue()),
                        isnull(lastrecord.getOiltempwarnvalue()),
                        isnull(lastrecord.getOiltempwarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[0].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastoiltempalarmstatus(),
                        lastrecord.getStyle().split(",")[0].split("\\|")[2]);
                array.add(currrecord.getLastoiltempalarmstatus());
                currrecord.setLasth2oalarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2o()),
                        isnull(lastrecord.getH2oalarmvalue()),
                        isnull(lastrecord.getH2oalarmrelievevalue()),
                        isnull(lastrecord.getH2owarnvalue()),
                        isnull(lastrecord.getH2owarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[1].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasth2oalarmstatus(),
                        lastrecord.getStyle().split(",")[1].split("\\|")[2]);
                array.add(currrecord.getLasth2oalarmstatus());
                currrecord.setLastgastempalarmstatus(GetAlarmStatus(isnull(currrecord.getLasttemperature()),
                        isnull(lastrecord.getTemperaturealarmvalue()),
                        isnull(lastrecord.getTemperaturealarmrelievevalue()),
                        isnull(lastrecord.getTemperaturewarnvalue()),
                        isnull(lastrecord.getTemperaturewarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[2].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastgastempalarmstatus(),
                        lastrecord.getStyle().split(",")[2].split("\\|")[2]);
                array.add(currrecord.getLastgastempalarmstatus());
                currrecord.setLasth2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasth2()),
                        isnull(lastrecord.getH2alarmvalue()),
                        isnull(lastrecord.getH2alarmrelievevalue()),
                        isnull(lastrecord.getH2warnvalue()),
                        isnull(lastrecord.getH2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[3].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasth2alarmstatus(),
                        lastrecord.getStyle().split(",")[3].split("\\|")[2]);
                array.add(currrecord.getLasth2alarmstatus());
                currrecord.setLastc2h2alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h2()),
                        isnull(lastrecord.getC2h2alarmvalue()),
                        isnull(lastrecord.getC2h2alarmrelievevalue()),
                        isnull(lastrecord.getC2h2warnvalue()),
                        isnull(lastrecord.getC2h2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[4].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h2alarmstatus(),
                        lastrecord.getStyle().split(",")[4].split("\\|")[2]);
                array.add(currrecord.getLastc2h2alarmstatus());
                currrecord.setLastc2h4alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h4()),
                        isnull(lastrecord.getC2h4alarmvalue()),
                        isnull(lastrecord.getC2h4alarmrelievevalue()),
                        isnull(lastrecord.getC2h4warnvalue()),
                        isnull(lastrecord.getC2h4warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[5].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h4alarmstatus(),
                        lastrecord.getStyle().split(",")[5].split("\\|")[2]);
                array.add(currrecord.getLastc2h4alarmstatus());
                currrecord.setLastcoalarmstatus(GetAlarmStatus(isnull(currrecord.getLastco()),
                        isnull(lastrecord.getCoalarmvalue()),
                        isnull(lastrecord.getCoalarmrelievevalue()),
                        isnull(lastrecord.getCowarnvalue()),
                        isnull(lastrecord.getCowarnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[6].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastcoalarmstatus(),
                        lastrecord.getStyle().split(",")[6].split("\\|")[2]);
                array.add(currrecord.getLastcoalarmstatus());
                currrecord.setLastco2alarmstatus(GetAlarmStatus(isnull(currrecord.getLastco2()),
                        isnull(lastrecord.getCo2alarmvalue()),
                        isnull(lastrecord.getCo2alarmrelievevalue()),
                        isnull(lastrecord.getCo2warnvalue()),
                        isnull(lastrecord.getCo2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[7].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastco2alarmstatus(),
                        lastrecord.getStyle().split(",")[7].split("\\|")[2]);
                array.add(currrecord.getLastco2alarmstatus());
                currrecord.setLastc2h6alarmstatus(GetAlarmStatus(isnull(currrecord.getLastc2h6()),
                        isnull(lastrecord.getC2h6alarmvalue()),
                        isnull(lastrecord.getC2h6alarmrelievevalue()),
                        isnull(lastrecord.getC2h6warnvalue()),
                        isnull(lastrecord.getC2h6warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[8].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastc2h6alarmstatus(),
                        lastrecord.getStyle().split(",")[8].split("\\|")[2]);
                array.add(currrecord.getLastc2h6alarmstatus());
                currrecord.setLastch4alarmstatus(GetAlarmStatus(isnull(currrecord.getLastch4()),
                        isnull(lastrecord.getCh4alarmvalue()),
                        isnull(lastrecord.getCh4alarmrelievevalue()),
                        isnull(lastrecord.getCh4warnvalue()),
                        isnull(lastrecord.getCh4warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[9].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastch4alarmstatus(),
                        lastrecord.getStyle().split(",")[9].split("\\|")[2]);
                array.add(currrecord.getLastch4alarmstatus());
                currrecord.setLasto2alarmstatus(GetAlarmStatus(isnull(currrecord.getLasto2()),
                        isnull(lastrecord.getO2alarmvalue()),
                        isnull(lastrecord.getO2alarmrelievevalue()),
                        isnull(lastrecord.getO2warnvalue()),
                        isnull(lastrecord.getO2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[10].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLasto2alarmstatus(),
                        lastrecord.getStyle().split(",")[10].split("\\|")[2]);
                array.add(currrecord.getLasto2alarmstatus());
                currrecord.setLastn2alarmstatus(GetAlarmStatus(isnull(currrecord.getLastn2()),
                        isnull(lastrecord.getN2alarmvalue()),
                        isnull(lastrecord.getN2alarmrelievevalue()),
                        isnull(lastrecord.getN2warnvalue()),
                        isnull(lastrecord.getN2warnrelievevalue()),
                        isnull(lastrecord.getLastalarmlevel()), true,
                        lastrecord.getStyle().split(",")[11].split("\\|")[2],
                        lastrecord.getDevicegroup(),
                        lastrecord.getDeviceaddr(),
                        lastrecord.getPointname()));
                alarmMessage += GetAlarmMessage(currrecord.getLastn2alarmstatus(),
                        lastrecord.getStyle().split(",")[11].split("\\|")[2]);
                array.add(currrecord.getLastn2alarmstatus());
            } catch (SocketException e) {
                if (Close()) {
                    communicationstatus = 1;
                    oilfxsensorServiceImpl.UpdateStatus(ad, 1);
                    ComRunner.netmap.remove(ad.getId());
                }
            } catch (Exception e) {
                setText("读取数据出错！" + e.toString());
                communicationstatus = 1;
                oilfxsensorServiceImpl.UpdateStatus(ad, 1);
            }

            alarmLevel = HexUtil.MAX(array);
            currrecord.setLastalarmlevel(alarmLevel);
            currrecord.setLastalarmmessage(alarmMessage);

            currrecord.setLastcommunicationstatus(communicationstatus);
            currrecord.setLastdatatime(new Date());
            if (communicationstatus == 0) {
                currrecord.setLasthisdatatime(addHistory(lastrecord));
                oilfxsensorServiceImpl.Update(currrecord, false);
            } else {
                oilfxsensorServiceImpl.UpdateStatus(ad, 1);
            }

            StringBuilder str2 = new StringBuilder();
            str2.append("【设备返回数据：】");
            str2.append(" 站点地址：" + currrecord.getStationid());
            str2.append(" 设备地址：" + currrecord.getDeviceaddr() + "\r\n");
            str2.append(" 油温：" + currrecord.getLastoiltemp() + "\r\n");
            str2.append(" 水温：" + currrecord.getLasttemperature() + "\r\n");
            str2.append(" 氢气：" + currrecord.getLasth2() + "\r\n");
            str2.append(" 一氧化碳：" + currrecord.getLastco() + "\r\n");
            str2.append(" 甲烷：" + currrecord.getLastch4() + "\r\n");
            str2.append(" 二氧化碳：" + currrecord.getLastco2() + "\r\n");
            str2.append(" 乙烯：" + currrecord.getLastc2h4() + "\r\n");
            str2.append(" 乙烷：" + currrecord.getLastc2h6() + "\r\n");
            str2.append(" 乙炔：" + currrecord.getLastc2h2() + "\r\n");
            str2.append(" 总烃：" + currrecord.getLasttothyd() + "\r\n");
            str2.append(" 微水：" + currrecord.getLastppm() + "\r\n");
            str2.append(" 氧气：" + currrecord.getLasto2() + "\r\n");
            str2.append(" 氮气：" + currrecord.getLastn2() + "\r\n");
            setText(str2.toString());
        }
    }

    void deelsendData(Appdevice Appdevice) throws Exception {
        String cstr = "";
        String cmdtype = "";
        String nametype = "";
        int timersp = (int) Math.round(Appdevice.getTimersp() * 1000);
        while (issend.get(Thread.currentThread().getId()) != null) {
            if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
                addrindex = Appdevice.getAddrmin();

            try {
                if (ad.getBrand().equals("DGA9")) {
                    cstr = String.format(Appdevice.getCmd(), String.format("%02X", addrindex));
                    cstr = cstr + CRC16.calcCrc16(cstr);
                    Send(cstr);
                } else {
                    //cstr = "01040000000A";
                    cstr = String.format("%02X", addrindex) +"0400000012";;
                    cstr = cstr + CRC16.calcCrc16(cstr);
                    Send(cstr);
                }

            } catch (Exception EX) {
                setText(EX.toString());
            }
            // //setText("向" + Appdevice.getName() + "设备发送指令" + cstr);
            reSend[addrindex]++;
            if (reSend[addrindex] > 3) {
                oilfxsensorServiceImpl.UpdateStatus(ad, addrindex, 1);
                reSend[addrindex] = 0;
                addrindex++;
            }
            Thread.sleep(timersp);
        }
    }

    @Async("threadPool")
    public void startSendThread(Appdevice Appdevice) throws Exception {
        checkType = "crc16";
        issend.put(Thread.currentThread().getId(), true);

        if (ad.getCommunicationmode() == 4) {
            // if(ad.getBrand().equals("DGA9"))

            // else

        } else {
            deelsendData(Appdevice);
        }

    }

    // @Async("threadPool")
    // public void startReceiveThread(){
    // isrecv.put(Thread.currentThread().getId(), true);
    // while (isrecv.get(Thread.currentThread().getId()) != null) {
    // //这里根据实际协议替换,解析方法recvCrc16;recvSum();
    // recv();
    // try {
    // Thread.sleep(interval);
    // } catch (InterruptedException e) {
    // e.printStackTrace();
    // }
    // }
    // }
    // //setText("向" + Appdevice.getName() + "设备发送指令" + cstr);
    // reSend[addrindex]++;
    // if (reSend[addrindex] > 3) {
    // oilfxsensorServiceImpl.UpdateStatus(ad, addrindex, 1);
    // reSend[addrindex] = 0;
    // addrindex++;
    // }
    // Thread.sleep(timersp);
    // }
    // }

    // @Async("threadPool")
    // public void startReceiveThread(){
    // isrecv.put(Thread.currentThread().getId(), true);
    // while (isrecv.get(Thread.currentThread().getId()) != null) {
    // //这里根据实际协议替换,解析方法recvCrc16;recvSum();
    // recv();
    // try {
    // Thread.sleep(interval);
    // } catch (InterruptedException e) {
    // e.printStackTrace();
    // }
    // }
    // }
}
